var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
import { signByMd5 } from "../../utils/util";
import checkLogin from '../../utils/checkLogin.js';
var gloalAudio = require("../common/gloalAudio/gloalAudio.js");//全局音乐模块
Page({
  data: { 
    isPlayGloalAudio: false,
    bannerList:[],
    classifyList: [],
    videoList: [],
    audioList: [],
    boutiqueList: [],
    openRedPack:true,
    showRedPack:false,
    hasRedPack:true,
    amount:'0.00',
    slogan:'祝您学习愉快',
    name:'',
    activityId:'',
    portrait:'../../images/logo.jpg'
  },
  onLoad(options) {
    console.log(options);
    this.getModuleData();
    // app.checkPlay(this);
    // 检查有没有登录，第二个参数是回调函数
    checkLogin.login(this,function(res){
      console.log(res)
      //查询有没有红包
      this.checkRedPack(res)
    })
    
  },
  onShow(){
    app.checkPlay(this);//检测全局有没有音频播放
  },
  //下拉刷新
  onPullDownRefresh() {
    this.getModuleData();
    // 检查有没有登录，第二个参数是回调函数
    checkLogin.login(this, function (res) {
      console.log(res)
    })
    app.checkPlay(this);//检测全局有没有音频播放
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  // 首页数据渲染
  getModuleData: function () {
    var param = {
    }
    ajaxhelper.get(app.globalData.frontJSHost + 'index/homepage', param, this,this.setmoduleData)
  },
  setmoduleData(res){
    console.log(res)
    var that=this;
    if (res.result) {
      if(!res.data){return false}
      var moduleList = res.data.moduleDTOList;
      var moduleListObj = moduleList.arrayToJson("moduleType");
      that.setData({
        bannerList: moduleListObj.index_Carousel,
        classifyList: moduleListObj.UNIVERSITY_ICON,
        videoList: moduleListObj.UNIVERSITY_VIDEO,
        audioList: moduleListObj.UNIVERSITY_AUDIO,
        boutiqueList: moduleListObj.KNOWLEDGE_BILLBOARD, //暂时  
      })
      console.log(moduleListObj)
    }else{
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    
  },
  // 去分类列表
  goClassfiyList(e){
    console.log(e)
    var type = e.currentTarget.dataset.type;
    if (type =='KNOWLEDGE_VIDEO_COLLECTION_LIST'){
      wx.navigateTo({
        url: '../videoList/videoList',
      })
    } else if (type =='KNOWLEDGE_AUDIO_COLLECTION_LIST'){
      wx.navigateTo({
        url: '../audioList/audioList',
      })
    } else if (type == 'KNOWLEDGE_FAVORITE_LIST'){
      wx.navigateTo({
        url: '../alSubscribe/alSubscribe',
      })
    }
  },
  // 更多音频
  goAudioList(){
    wx.navigateTo({
      url: '../audioList/audioList',
    })
  },
  // 精品列表
  goBoutiqueMore(){
    wx.navigateTo({
      url: '../bestsellerList/bestsellerList',
    })
  },
  // 专辑详情
  goAudioAlbumDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../audioAlbumDetail/audioAlbumDetail?id='+id,
    })
  },
  // 视频列表
  goVideoList() {
    wx.navigateTo({
      url: '../videoList/videoList',
    })
  },
  // 视频专辑详情
  goVideoAlbumDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../videoAlbumDetail/videoAlbumDetail?id='+id,
    })
  },
  // 全局音乐弹框
  showGloalAudio() {
    gloalAudio.set('', {
      confirm: function (data) {
        console.log(data)
      }.bind(this)
    });
    gloalAudio.open('')
  },
  // 分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '结果大学',
      path: '/pages/index/index',
      // imageUrl: cover,//测试 使用截图
      success: function (res) {
        // 转发成功
        console.log(res)
      },
      fail: function (res) {
        // 转发失败
        console.log(res)
      }
    }
  },
  //以下是红包相关
  checkRedPack: function (token) {
    var param={};
    var token = token|| '';
    ajaxhelper.get(app.globalData.frontJSHost + 'hongbao/try?token=' + encodeURIComponent(token), param, this, function (res) {
      console.log(res);
      if (res.resultStatus == 200) {
        var Data = res.data;
        if (Data==null){
          this.setData({
            hasRedPack: false
          })
        }else{
          this.showRedPack();
          console.log((Data.hongbaoActivity.amount / 100).toFixed(2));
          this.setData({
            hasRedPack: true,
            portrait: Data.hongbaoActivity.pic,
            amount: (Data.hongbaoActivity.amount/100).toFixed(2),
            slogan: Data.hongbaoActivity.slogan,
            activityId: Data.hongbaoActivity.id,
            name: Data.hongbaoActivity.name
          })
        }
        
      } else {
        // wx.showToast({
        //   title: !!res.message.global ? res.message.global : "请求出错",
        //   icon: "loading"
        // })
        this.setData({
          hasRedPack: false
        })
      }
    })
  },
  showRedPack:function(){
    this.setData({
      showRedPack: true
    })
  },
  openIt:function(){
    var that=this;
    var id=that.data.activityId;
    console.log('open');
    setTimeout(function(){
      that.setData({
        openRedPack: false,
      })
    },1000) 
   //领取红包请求
    var param = {}
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    ajaxhelper.get(app.globalData.frontJSHost + 'hongbao/get/' + id + '?token=' + encodeURIComponent(token), param, this, function (res) {
      if (res.result == true) {
      } else {
        wx.showToast({
          title: !!res.message.global ? res.message.global : "请求出错",
          icon: "loading"
        })
      }
    })
  },
 closeIt:function(){
   this.setData({
     showRedPack: false
   })
 },
 enter:function(){
   wx.navigateTo({
     url: '../myRedPack/myRedPack',
   })
 },
//  轮播图跳转到其他页面
 goOtherPage(e){
   var type = e.currentTarget.dataset.type;
   var id = e.currentTarget.dataset.id;
   if (type == 'H5_DETAIL'){
     if (id == 'boss'){
       //  跳转到老板的信
       wx.navigateTo({
         url: '../BossLetter/BossLetter',
       })
     } else if (id == 'image') {
       wx.navigateTo({
         url: '../otherImgPage/otherImgPage',
       })
     }
     
   }
   
 }
 
})
